package com.eastcom.dataquality.dv.analysis.extract.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.eastcom.canicula.stope.utils.DateUtil;
import com.eastcom.dataquality.core.dao.RawDataDao;
import com.eastcom.dataquality.core.module.Atom_RawData;
import com.eastcom.dataquality.core.util.UUIDHexGenerator;
import com.eastcom.dataquality.dv.CollectData;
import com.eastcom.dataquality.dv.KpiData;
import com.eastcom.dataquality.dv.analysis.extract.Extractor;

public class RawDataExtractor extends Extractor{
	
	private UUIDHexGenerator uuid=new UUIDHexGenerator();
	
	private static Logger logger=Logger.getLogger(RawDataExtractor.class);
	
	public void process(CollectData collectData){
		logger.info("dataset_id:"+collectData.getDataset_id()+" dataTime:"+DateUtil.DateToString(collectData.getDataTime())+" begin insert rawData...");
		List<KpiData> kpiDatas=collectData.getKpiDatas();
		List<Atom_RawData> rawDatas=wrap(kpiDatas);
		insert(rawDatas);
		logger.info("dataset_id:"+collectData.getDataset_id()+" dataTime:"+DateUtil.DateToString(collectData.getDataTime())+" end insert rawData total:"+rawDatas.size());
	}

	private void insert(List<Atom_RawData> rawDatas) {
		RawDataDao rawDataDao=daoFactory.getRawDataDao();
		rawDataDao.insert(rawDatas);
	}

	private List<Atom_RawData> wrap(List<KpiData> kpiDatas) {
		List<Atom_RawData> ret=new ArrayList<Atom_RawData>();
		for(KpiData kpiData:kpiDatas){
			Atom_RawData rawData=wrapForKpiData(kpiData);
			ret.add(rawData);
		}
		return ret;
	}

	private Atom_RawData wrapForKpiData(KpiData kpiData) {
		
		Atom_RawData rawData=new Atom_RawData();
		rawData.setTimeStamp(kpiData.getDataTime());
		rawData.setDatasetId(kpiData.getDataset_id());
		rawData.setNeId(kpiData.getDimension());
		rawData.setKpiId(kpiData.getKpiName());
		rawData.setCollectorDate(System.currentTimeMillis());
		rawData.setValue(kpiData.getKpiValue());
		rawData.setBaselineUp(-1d);
		rawData.setBaselineDown(-1d);
		rawData.setValidationType("");
		rawData.setIsNormal("1");
		return rawData;
	}
}
